import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:xihuwenti_tickets/net/post_request.dart';
import 'package:xihuwenti_tickets/net/response/venue_list_response.dart';
import 'package:html/parser.dart' show parse;
import 'package:xihuwenti_tickets/ui/select_site_page.dart';


class GetPlaceViewModel extends BaseViewModel{
  GetPlaceViewModel(BuildContext context) : super(context);

  var list = <VenueListBean>[];

  void getPlaceHtmlData(){
    showLoadingState();
    PostRequest.getPlaceHtml((response) {
      VenueListResponse bean = VenueListResponse.fromJson(response.toString().toMap());
      if(1==bean.code){
        list = bean.data;
        showContent();
        notifyListeners();
      }else{
        showErrorState(bean.msg);
        showToast(bean.msg);
      }

    }, (error) {
      showErrorState(error);
      showToast(error);
    });
  }

  void getDetail(String id){
    showLoadingDialog();
    PostRequest.getPlaceDetailHtml(id, (response){
      hideDialog();
      _getInfoData(response.toString());
    }, (error){
      hideDialog();
    });
  }

  void _getInfoData(String data){
    var document = parse(data);
    var body = document.querySelector("body");
    var elementsByClassName = body.getElementsByClassName("item");
    for(var item in elementsByClassName){
      if(item.outerHtml.contains("羽毛球")){
        var href = item.getElementsByClassName("booking")[0].attributes["href"];
        var id = href.split("=")[1].split("&")[0];
        // Go().pushName("/detail",params: {"id":id.toString()});
        Navigator.pushNamed(context, "/detail",arguments: {"id":id.toString()});
        break;
      }
    }
  }

  @override
  void onCreated() {
    super.onCreated();
    getPlaceHtmlData();
  }

}